#include "bridget_communicator.h"


void
bridget_communicator::initCom()
{
	 WMI_ConnectionManager::initCOMSecurityConfiguration();
}

 void bridget_communicator::closeCom()
 {
	 WMI_ConnectionManager::closeCOMSecurityConfiguration();
 }



WMI_HResult 
bridget_communicator::save(int idComputer, CM_ResultCollection& resultCollection)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();
	WMI_HResult whres;
	CM_ResultCollection CMresultCollection;

    int res = dbManager.saveResultCollection(idComputer, resultCollection);

	return whres;
}

WMI_HResult 
bridget_communicator::save(const wstring& dc, const wstring& fqdn, vector<ADCM_Computer>& computers)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();

	ADCM_Domain adcmDomain(wstring(dc.c_str()), wstring(fqdn.c_str()));
	dbManager.saveDomain(adcmDomain);

	WMI_HResult whres;
	int res = 0;
	
	if(adcmDomain.getDomainID() > 0)
	{
		for(vector<ADCM_Computer>::iterator it = computers.begin(); it != computers.end(); it++)
		{
			whres.setHRESULT(HRESULT(dbManager.saveComputer(adcmDomain.getDomainID(), *it)));
		}
	}
	else
	{
		res = 1;
	}
	
	whres.setHRESULT((HRESULT(res)));

	return whres;
}

WMI_HResult 
bridget_communicator::getDifferences(int idComputer, CM_ResultCollection& resultCollection)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();

	CM_ResultCollection current;
	CM_ResultCollection beforeCurrent;
	dbManager.getLastCollection(idComputer, current);
	dbManager.getBeforeLastCollection(idComputer, beforeCurrent);

	WMI_Result<WMI_Process> differencesProcess;

    bc_utils::fillDifferences(current.getProcesses(), beforeCurrent.getProcesses(), resultCollection.getProcesses());

    bc_utils::fillDifferences(current.getShares(), beforeCurrent.getShares(), resultCollection.getShares());

    bc_utils::fillRegistryDifferences(current.getRegistries(), beforeCurrent.getRegistries(), resultCollection.getRegistries());  	
    
	WMI_HResult res;

	return res;
}

WMI_HResult 
bridget_communicator::getComputers(wstring& dc, wstring& fqdn, wstring& user, 
			 wstring& password, vector<ADCM_Computer>& computers)
{
	ADCM_Domain adcmDomain(dc, fqdn);
	WMI_HResult whres;
	int res = adcmDomain.loadComputers(user, password, computers);
	whres.setHRESULT(HRESULT(res));

	return whres;
}

WMI_HResult 
bridget_communicator::getComputersResult(int idComputer, CM_ResultCollection* resultCollection, wstring& user, 
											wstring& password, wstring& host, wstring& fqdn, bool localActive)
{
	
	WMI_HResult whres;

	wstring userName;

	if (!localActive)
	{
    userName.append(fqdn);
    userName.append(L"\\");
    userName.append(user);
	} else
	{
		userName.append(L"");
	}

	WMI_Connection wmiConnect, wmiConnectReg;

	bc_utils::initConnection(wmiConnect, userName, password, host, false, localActive);
	bc_utils::initConnection(wmiConnectReg, userName, password, host, true, localActive);

	bc_utils::getProcess(wmiConnect, *resultCollection);
	bc_utils::getShares(wmiConnect, *resultCollection);
	bc_utils::getFileDirectory(wmiConnect, WINDOWS_DIR,*resultCollection);

	bc_utils::getRegistries(wmiConnectReg, RSOFTWAREPATH, *resultCollection);

	

	wmiConnect.close();
//	wmiConnectReg.close();

	/*CMresultCollection.setShares(resultCollection.getShares());
	CMresultCollection.setProcesses(resultCollection.getProcesses());
	CMresultCollection.setRegistries(resultCollection.getRegistries());
	CMresultCollection.setFileDirs(resultCollection.getFileDirs());*/

	//int res = dbManager.getLastCollection(idComputer, resultCollection);

	/*resultCollection.setShares(CMresultCollection.getShares());
	resultCollection.setProcesses(CMresultCollection.getProcesses());
	resultCollection.setRegistries(CMresultCollection.getRegistries());
	resultCollection.setFileDirs(CMresultCollection.getFileDirs());*/

	//whres.setHRESULT(HRESULT(0));
WMI_HResult *w= new WMI_HResult(L"",L"",0);
	return *w;
delete w;
}

WMI_HResult 
bridget_communicator::getComputers(const wstring fqdn, vector<ADCM_Computer>& computersDB)
{
	CM_DBLiteManager dbManager;
	dbManager.initialize();

	WMI_HResult* whres = new WMI_HResult();

	int res = dbManager.getComputers(fqdn, computersDB);

	(*whres).setHRESULT(HRESULT(res));

	return *whres;
}

